/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      RankedDocument.hpp
 *
 * $FTS$
 */
#ifndef _RANKED_DOCUMENT_HPP__
#define _RANKED_DOCUMENT_HPP__ 1

/**
  @file Ranker.hpp
  @brief Fulltext search document ranker
*/
#include <new>

#include <DocumentInfo.hpp>
#include <Passage.hpp>
#include <STLFunctional.hpp>

#include <stdio.h>

namespace FTSS // FullTextSearchSystem
{

/**
  @struct RankedPassage RankedDocument.hpp <RankedDocument.hpp>
  @brief Ranked passage (start of passage and end)
*/
struct Snippet
{
private:
	/**
	  @brief Default constructor
	*/
	Snippet();
public:
	/** No position */
	static const UINT_32 npos;

	/** Max position in base points array       */
	UINT_32                     max_base_points;

	/** Max used position in base points array  */
	UINT_32                     base_points_num;

	/** Base points                             */
	UINT_32                   * base_points;

	/**
	  @brief Constructor
	*/
	inline Snippet(const UINT_32 & iMaxBasePoints): max_base_points(iMaxBasePoints), base_points_num(0)
	{
		base_points = new UINT_32[max_base_points];
	}

	inline Snippet(const Snippet & oRhs)
	{
		max_base_points = oRhs.max_base_points;
		base_points_num = oRhs.base_points_num;

		base_points = new UINT_32[max_base_points];
		for (UINT_32 iI = 0; iI < base_points_num; ++iI) { base_points[iI] = oRhs.base_points[iI]; }
	}

	inline Snippet & operator=(const Snippet & oRhs)
	{
		if (this != &oRhs)
		{
			base_points_num = oRhs.base_points_num;
			if (max_base_points != oRhs.max_base_points)
			{
				max_base_points = oRhs.max_base_points;

				delete [] base_points;

				base_points = new UINT_32[max_base_points];
			}
			for (UINT_32 iI = 0; iI < base_points_num; ++iI) { base_points[iI] = oRhs.base_points[iI]; }
		}
	return *this;
	}

	/**
	  @brief A destructor
	*/
	inline ~Snippet() throw()
	{
		delete [] base_points;
	}

	inline void PrintSnippet() const
	{
		fprintf(stderr, "base_points_num %d, max_base_points %d {", base_points_num, max_base_points);
		for (UINT_32 iI = 0; iI < base_points_num; ++iI)
		{
			fprintf(stderr, " %d", base_points[iI]);
		}
		fprintf(stderr, "}\n");
	}
};

/**
  @struct RankedDocument RankedDocument.hpp <RankedDocument.hpp>
  @brief Ranked document
*/
struct RankedDocument
{
public:
	/** Document properties     */
	DocumentInfo         * document_info;
	/** Passages in document    */
	Snippet              * snippets;
	/** Number of used passages */
	UINT_32                max_snippets;
	/** Number of used passages */
	UINT_32                snippets_num;
	/** Rank of document        */
	W_FLOAT                rank;

	/**
	  @brief Default constructor
	*/
	inline RankedDocument(): document_info(NULL), snippets(NULL), max_snippets(0), snippets_num(0), rank(0.0)
	{
		;;
	}

	/**
	  @brief Construct structure of rankde document
	  @param iSnippets - number of snippets
	  @param iSnippetSize - max. snippet size
	*/
	void Construct(const UINT_32 & iSnippets, const UINT_32 & iSnippetSize)
	{
		max_snippets = iSnippets;

		snippets = (Snippet *) operator new(max_snippets * sizeof(Snippet));
		for (UINT_32 iI = 0; iI < max_snippets; ++iI) { ::new(&snippets[iI]) Snippet(iSnippetSize); }
	}

	/**
	  @brief Copy constructor
	  @param oRhs - object to copy
	*/
	inline RankedDocument(const RankedDocument & oRhs): snippets(NULL)
	{
		document_info   = oRhs.document_info;
		max_snippets    = oRhs.max_snippets;
		snippets_num    = oRhs.snippets_num;
		rank            = oRhs.rank;

		snippets = (Snippet *) operator new(max_snippets * sizeof(Snippet));
		for (UINT_32 iI = 0; iI < max_snippets; ++iI) { ::new(&snippets[iI]) Snippet(oRhs.snippets[iI]); }
	}

	/**
	  @brief Copy operator
	  @param oRhs - object to copy
	  @return reference to self
	*/
	RankedDocument & operator=(const RankedDocument & oRhs);

	/**
	  @brief A destructor
	*/
	inline ~RankedDocument() throw()
	{
		if (snippets != NULL)
		{
			for (UINT_32 iI = 0; iI < max_snippets; ++iI) { snippets[iI].~Snippet(); }
			operator delete(snippets);
		}
	}

	inline void PrintDocument() const
	{
		fprintf(stderr, "snippets_num %d, max_snippets %d, rank %f {\n", snippets_num, max_snippets, rank);
		for (UINT_32 iI = 0; iI < max_snippets; ++iI)
		{
			snippets[iI].PrintSnippet();
		}
		fprintf(stderr, "}\n");

	}
};

} // namespace FTSS
#endif // _RANKED_DOCUMENT_HPP__
// End.
